//
//  myBunkController.m
//  SSSAllRollAgency
//
//  Created by Darker on 2017/3/15.
//  Copyright © 2017年 Darker. All rights reserved.
//

#import "myBunkController.h"
#import "bunkViewCell.h"
#import "addBunkCardController.h"
#import "myBunkCardModel.h"

@interface myBunkController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_bunkTableView;
    //NSMutableArray *_dataArray;
    myBunkCardModel *currenSelectModel;//拿到当前模型
    NSString *_myBunkStr;
    NSString *_bankCardId;
}
@property (nonatomic,strong)NSMutableArray *dataArray;//数据源
@end
static NSString *bunkID    = @"bunkID";
@implementation myBunkController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.title = @"我的银行卡";
    //去掉透明后导航栏下边的黑边
    //    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpApperance];
    [self loadMyBunkCarData];
   
}
- (void)loadMyBunkCarData{
    
    //NSString *myBunkUrl = [NSString stringWithFormat:@"%@%@",SSSCommomUrl,@"user/myBankCard"];
    ZD_WEAKSELF;
    [QMUITips showLoading:@"加载中..." detailText:nil inView:self.view];
    [[XHRequestManager manager] getRequest:kConfigPath(kMyBunkCard) params:nil success:^(id  _Nonnull responseObj) {
        
        XHLog(@"myBunk----%@",responseObj);
        if ([responseObj[@"status"] intValue] == 0) {
            
            [QMUITips hideAllToastInView:self.view animated:YES];
            
            //[QMUITips showSucceed:@"加载成功" inView:self.view hideAfterDelay:0.5];
            
            if ([responseObj[@"data"] count] == 0) {
                
                showTipHudStr(@"还没绑定银行卡");
                return ;
            }
            
            weakSelf.dataArray = [myBunkCardModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"]];
            //刷新表格
            dispatch_async(dispatch_get_main_queue(), ^{
                //刷新表格
                [_bunkTableView reloadData];
            });
            
        }else{
            
            [QMUITips hideAllToastInView:self.view animated:YES];
            [QMUITips showError:responseObj[@"message"] inView:self.view hideAfterDelay:2];
            
        }

    } failure:^(NSError * _Nonnull error) {
        
        [QMUITips hideAllToastInView:self.view animated:YES];
        XHLog(@"error--%@",error.localizedDescription);
        [QMUITips showError:error.localizedDescription inView:self.view hideAfterDelay:2];
        
    }];
    
//    [ZDNetManager zd_requestWithType:ZDHttpRequestTypeGet UrlString:kConfigPath(kMyBunkCard) Parameters:nil SuccessBlock:^(id response) {
//        
//        XHLog(@"myBunk----%@",response);
//        if ([response[@"status"] intValue] == 0) {
//            [QMUITips showSucceed:@"加载成功" inView:self.view hideAfterDelay:0.5];
//            weakSelf.dataArray = [myBunkCardModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
//            //刷新表格
//            dispatch_async(dispatch_get_main_queue(), ^{
//                //刷新表格
//                [_bunkTableView reloadData];
//            });
//            
//        }else{
//            [QMUITips hideAllToastInView:self.view animated:YES];
//            [QMUITips showError:response[@"message"] inView:self.view hideAfterDelay:2];
//            
//        }
//        
//    } FailureBlock:^(NSError *error) {
//        [QMUITips hideAllToastInView:self.view animated:YES];
//        XHLog(@"error--%@",error.localizedDescription);
//        [QMUITips showError:error.localizedDescription inView:self.view hideAfterDelay:2];
//        
//    } progress:^(int64_t bytesProgress, int64_t totalBytesProgress) {
//        
//        
//    }];

    
}
- (void)setUpApperance{
    
    //添加背景图
    UIImageView *bgImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg_1"]];
    bgImageView.userInteractionEnabled = YES;
    [self.view addSubview:bgImageView];
    bgImageView.frame = self.view.frame;
    //导航栏按钮
    self.navigationItem.rightBarButtonItem=[UIBarButtonItem itemWithTitle:@"添加新卡" color:[UIColor colorWithHex:@"FFFFFF"] target:self action:@selector(toAddBunkController)];
    
    _bunkTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,64+ HeigthIos6CompeteWithCurrent(14), KSCREEN_Width, HeigthIos6CompeteWithCurrent(150)) style:UITableViewStylePlain];
    _bunkTableView.delegate = self;
    _bunkTableView.dataSource = self;
    _bunkTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _bunkTableView.bounces = NO;
    _bunkTableView.backgroundColor = [UIColor clearColor];
    [bgImageView addSubview:_bunkTableView];
    _bunkTableView.rowHeight = HeigthIos6CompeteWithCurrent(50);
    _bunkTableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Class currentClass = [bunkViewCell class];
    
    bunkViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(currentClass)];
    if (!cell) {
        cell = [[bunkViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:bunkID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.myBunkModel = _dataArray[indexPath.row];
    
    return cell;
    
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
//定义编辑样式为删除样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
//设置返回存放侧滑按钮数组
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    currenSelectModel = _dataArray[indexPath.row];
    
    _myBunkStr = currenSelectModel.bankCardNo;
    _bankCardId = [NSString stringWithFormat:@"%ld",(long)currenSelectModel.ID];
    
    //这是iOS8以后的方法
    UITableViewRowAction *deleBtn = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"解除绑定" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        //[_dataArray removeObjectAtIndex:indexPath.row];
        
        //[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
        //[_bunkTableView reloadData];
        [self delectBunk];
        
    }];
    
//    
//    UITableViewRowAction *moreBtn = [UITableViewRowAction  rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"更多更多" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//        
//        NSLog(@"更多，，点了");
//        
//    }];
//    
//    
//    UITableViewRowAction *upBtn = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//        
//        [_dataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
//        
//        NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
//        
//        [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
//        
//    }];
    
    //设置背景颜色，他们的大小会分局文字内容自适应，所以不用担心
    deleBtn.backgroundColor = [UIColor colorWithHex:@"46B67C"];
    
//    moreBtn.backgroundColor = [UIColor orangeColor];
//    
//    upBtn.backgroundColor = [UIColor grayColor];
    
    
    return @[deleBtn];
    
}
#pragma mark - 删除银行卡
- (void)delectBunk{
    //参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"bankCardId"] = _bankCardId;
    
    //NSString *verifcationUrl = [NSString stringWithFormat:@"%@%@%@%@",SSSCommomUrl,@"user/",_myBunkStr,@"/deleteBankCard"];
    //ZD_WEAKSELF;
    
    [[XHRequestManager manager] deleteRequest:kConfigPath(kDeleteBunkCard) params:params success:^(id  _Nonnull responseObj) {
        
        if ([responseObj[@"status"] intValue] == 0) {
            
            [QMUITips showSucceed:@"解除成功" inView:self.view hideAfterDelay:2];
            //weakSelf.dataArray = [[NSArray modelArrayWithClass:[MyBunkCardModel class] json:response[@"data"]] mutableCopy];
            [_dataArray removeObject:currenSelectModel];
            
            [_bunkTableView reloadData];
            
        }else{
            
            [QMUITips showError:responseObj[@"message"] inView:self.view hideAfterDelay:2];
        }

        
    } failure:^(NSError * _Nonnull error) {
        
        [QMUITips showError:error.localizedDescription inView:self.view hideAfterDelay:2];

    }];
    
    
//    [ZDNetManager zd_requestWithType:ZDHttpRequestTypeDelete UrlString:kConfigPath(kDeleteBunkCard) Parameters:nil SuccessBlock:^(id response) {
//        
//        if ([response[@"status"] intValue] == 0) {
//            
//            [QMUITips showSucceed:@"解除成功" inView:self.view hideAfterDelay:2];
//            //weakSelf.dataArray = [[NSArray modelArrayWithClass:[MyBunkCardModel class] json:response[@"data"]] mutableCopy];
//            [_dataArray removeObject:currenSelectModel];
//            
//            [_bunkTableView reloadData];
//            
//        }else{
//            
//            [QMUITips showError:response[@"message"] inView:self.view hideAfterDelay:2];
//        }
//        
//        
//    } FailureBlock:^(NSError *error) {
//        
//        [QMUITips showError:error.localizedDescription inView:self.view hideAfterDelay:2];
//        
//    } progress:^(int64_t bytesProgress, int64_t totalBytesProgress) {
//        
//        
//    }];
}

- (void)toAddBunkController{
    
//    if (_dataArray.count>0) {
//        
//        [QMUITips showError:@"只能绑定一张银行卡" inView:self.view hideAfterDelay:2];
//        return;
//    }else{
//        
//        addBunkCardController *addBunkVc = [[addBunkCardController alloc]init];
//        [self.navigationController pushViewController:addBunkVc animated:YES];
//    }

    
    addBunkCardController *addBunkVc = [[addBunkCardController alloc]init];
    [self.navigationController pushViewController:addBunkVc animated:YES];
    
}
- (NSMutableArray *)dataArray{
    
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
